package org.jeecg.modules.yusuannz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.system.entity.SysCategory;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.mapper.SysCategoryMapper;
import org.jeecg.modules.system.mapper.SysDepartMapper;
import org.jeecg.modules.yusuannz.entity.CaiwuBiaozhunNz;
import org.jeecg.modules.yusuannz.entity.CaiwuBiaozhunNz;
import org.jeecg.modules.yusuannz.entity.CaiwuLiuyongNz;
import org.jeecg.modules.yusuannz.entity.NianDuHuiZongNz;
import org.jeecg.modules.yusuannz.mapper.*;
import org.jeecg.modules.yusuannz.service.ICaiwuBiaozhunNzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: caiwu_yusuan
 * @Author: jeecg-boot
 * @Date:   2020-06-15
 * @Version: V1.0
 */
@Service
public class CaiwuBiaozhunNzServiceImpl extends ServiceImpl<CaiwuBiaozhunNzMapper, CaiwuBiaozhunNz> implements ICaiwuBiaozhunNzService {

    @Autowired
    private SysDepartMapper sysDepartMapper;
    @Autowired
    private SysCategoryMapper sysCategoryMapper;
    @Autowired
    private CaiwuLiuyongNzMapper CaiwuLiuyongNzMapper;
    @Autowired
    private CaiwuBiaozhunNzMapper CaiwuBiaozhunNzMapper;
    @Autowired
    private CaiwuXiangmuNzMapper CaiwuXiangmuNzMapper;
    @Autowired
    private CaiwuZhijieNzMapper CaiwuZhijieNzMapper;
    @Autowired
    private CaiwuShangnianNzMapper CaiwuShangnianNzMapper;


    @Override
    public List<CaiwuBiaozhunNz> listData(String deptId, String pid) {
        return this.baseMapper.listData(deptId,pid);
    }

    @Override
    public List<NianDuHuiZongNz> nianduhuizong(String orgCode) {
        List<NianDuHuiZongNz> retList = new ArrayList<>();
        Map<String,NianDuHuiZongNz> map = new HashMap<>();
        String deptId = this.sysDepartMapper.selectOne(new LambdaQueryWrapper<SysDepart>().eq(SysDepart::getOrgCode,orgCode)).getId();
        List<SysDepart> departList = this.sysDepartMapper.getSubOrgCodeByPcode(orgCode);

        List<SysCategory> categoryList = this.sysCategoryMapper.selectList(new LambdaQueryWrapper<SysCategory>().eq(SysCategory::getPid,"1270528547133616130"));

        if(CollectionUtils.isNotEmpty(categoryList) ){
            categoryList.forEach(sysCategory -> {
                NianDuHuiZongNz NianDuHuiZongNz = new NianDuHuiZongNz();
                NianDuHuiZongNz.setCategoryId(sysCategory.getId());
                NianDuHuiZongNz.setCategoryName(sysCategory.getName());
                NianDuHuiZongNz.setIsLeaf(null == sysCategory.getHasChild());
                retList.add(NianDuHuiZongNz);
                map.put(NianDuHuiZongNz.getCategoryId(),NianDuHuiZongNz);
                loadAllCategoryChildren(sysCategory,NianDuHuiZongNz,map);
            });

            //收入
            List<Map> biaozhunList = this.CaiwuBiaozhunNzMapper.shenheshu(deptId);
            List<Map> xiangmuList = this.CaiwuXiangmuNzMapper.shenheshu(deptId);
            List<Map> liuyongList = this.CaiwuLiuyongNzMapper.shenheshu(deptId);
            List<Map> shangnianList = this.CaiwuShangnianNzMapper.shenheshu(deptId);

            list2Niandu(biaozhunList,"biaozhun",map);
            list2Niandu(xiangmuList,"xiangmu",map);
            list2Niandu(liuyongList,"liuyong",map);
            list2Niandu(shangnianList,"shangnian",map);

            //支出
            List<Map> zhijieList = this.CaiwuZhijieNzMapper.shenheshu(deptId);
            list2Niandu(zhijieList,"zhijie",map);

            if(CollectionUtils.isEmpty(departList))
                return retList;

            List<Map> dxBiaozhunList = this.CaiwuBiaozhunNzMapper.duixiaShenheshu(departList);
            List<Map> dxXiangmuList = this.CaiwuXiangmuNzMapper.duixiaShenheshu(departList);
            list2Niandu(dxBiaozhunList,"dxbiaozhun",map);
            list2Niandu(dxXiangmuList,"dxxiangmu",map);
        }


        return retList;
    }

    private void loadAllCategoryChildren(SysCategory category,NianDuHuiZongNz NianDuHuiZongNz,Map<String,NianDuHuiZongNz> map) {
        if(null != category) {
            List<SysCategory> categoryList = this.sysCategoryMapper.selectList(new LambdaQueryWrapper<SysCategory>().eq(SysCategory::getPid, category.getId()));
            if(CollectionUtils.isNotEmpty(categoryList)){
                List<NianDuHuiZongNz> tmpList = new ArrayList<>();
                NianDuHuiZongNz.setChildren(tmpList);
                categoryList.forEach(sysCategory -> {
                    NianDuHuiZongNz tmp = new NianDuHuiZongNz();
                    tmp.setCategoryId(sysCategory.getId());
                    tmp.setCategoryName(sysCategory.getName());
                    tmp.setIsLeaf(null == sysCategory.getHasChild());
                    tmpList.add(tmp);
                    map.put(tmp.getCategoryId(),tmp);
                    loadAllCategoryChildren(sysCategory,tmp,map);
                });
            }
        }
    }

    private void list2Niandu(List<Map> list,String type,Map<String,NianDuHuiZongNz> map){
        if(CollectionUtils.isNotEmpty(list)){
            list.forEach(item->{
                String shenheshu = (null == item.get("shenheshu")?"0":item.get("shenheshu").toString());
                String categoryId = (String)item.get("categoryId");
                NianDuHuiZongNz NianDuHuiZongNz = map.get(categoryId);
                if(StringUtils.equals("biaozhun",type))
                    NianDuHuiZongNz.setSrBiaozhun(shenheshu);
                if(StringUtils.equals("xiangmu",type))
                    NianDuHuiZongNz.setSrXiangmu(shenheshu);
                if(StringUtils.equals("liuyong",type))
                    NianDuHuiZongNz.setSrLiuyong(shenheshu);
                if(StringUtils.equals("shangnian",type))
                    NianDuHuiZongNz.setSrShangnian(shenheshu);
                if(StringUtils.equals("zhijie",type))
                    NianDuHuiZongNz.setZcZhijie(shenheshu);
                if(StringUtils.equals("dxbiaozhun",type))
                    NianDuHuiZongNz.setZcBiaozhun(shenheshu);
                if(StringUtils.equals("dxxiangmu",type))
                    NianDuHuiZongNz.setZcXiangmu(shenheshu);
            });
        }
    }
}
